package org.yoki.edu.bg.radius.core.server.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.yoki.edu.bg.radius.core.server.dto.ServerPageQueryDTO;
import org.yoki.edu.bg.radius.core.server.infra.ServerRepository;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.AbstractEntity;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.Shift;
import com.yunzhitx.cloud.common.model.exception.rest.BasicRestStatus;
import lombok.Data;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;


import javax.persistence.Column;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;

/**
 * 代码生成器自动生成
 * Date:2018-1-31 15:03:14
 *
 * @author
 */
@Data
@Table(name = "radmiddle_server")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Server extends AbstractEntity<ServerRepository> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 操作人员Id
     */
    @Column(name = "fd_operatorId")
    private Integer operatorId;
    /**
     * 操作人名称
     */
    @Column(name = "fd_operatorName")
    private String operatorName;
    /**
     * 名称
     */
    @Column(name = "fd_name")
    private String name;
    /**
     * IP地址
     */
    @Column(name = "fd_ipAddr")
    private String ipAddr;
    /**
     * 密钥
     */
    @Column(name = "fd_secret")
    private String secret;
    /**
     * 是否正常，是：1(true)，否：0(false)
     */
    @Column(name = "fd_isWork")
    private Boolean isWork;

    public Server() {
    }

    public Server(Integer id) {
        setId(id);
    }

    @Override
    public Serializable getKey() {
        return getId();
    }

    @Override
    public boolean existed() {
        return false;
    }

    private static ServerRepository serverRepository;

    public static ServerRepository getServerRepository() {
        if (null == serverRepository) {
            serverRepository = InstanceFactory.getInstance(ServerRepository.class);
        }
        return serverRepository;
    }

    /**
     * @param ipAddr
     * @return
     */
    public static Boolean existedByIpAddr(String ipAddr) {
        ipAddr = StringUtil.isEmpty(ipAddr) ? null : ipAddr;
        Example e = new Example(Server.class);
        e.createCriteria().andEqualTo("ipAddr", ipAddr);
        Integer count = getServerRepository().selectCountByExample(e);
        if (null != count && count > 0) {
            return true;
        }
        return false;
    }

    public static Server findExistedByIpAddr(String ipAddr) {
        if (StringUtil.isEmpty(ipAddr)) {
            ipAddr = null;
        }
        Example e = new Example(Server.class);
        e.createCriteria().andEqualTo("ipAddr", ipAddr);
        List<Server> list = getServerRepository().selectByExample(e);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /* ******************************************************************************* *
                                            增
     * ******************************************************************************* */

    public void save() {
        if (existedByIpAddr(this.getIpAddr())) {
            Shift.fatal(BasicRestStatus.repeatDataErrorStatus);
        }
        getServerRepository().insert(this);
    }

    /* ******************************************************************************* *
                                            删
     * ******************************************************************************* */

    /**
     * 根据ID删除组件<br>
     * 同时删除所有的组件参数、组件内元件参数
     *
     * @param id
     */
    public static void deleteById(Integer id) {
        if (null == id) {
            Shift.fatal(BasicRestStatus.paramNullErrorStatus);
        }
        getServerRepository().deleteByPrimaryKey(id);
    }

    public static void deleteByIpAddr(String ipAddr) {
        if (null == ipAddr || ipAddr.length() <= 0) {
            Shift.fatal(BasicRestStatus.paramNullErrorStatus);
        }
        Example e = new Example(Server.class);
        e.createCriteria().andEqualTo("ipAddr", ipAddr);
        getServerRepository().deleteByExample(e);
    }

    /* ******************************************************************************* *
                                            改
     * ******************************************************************************* */

    /**
     * 根据Id，更新Server
     *
     * @param
     * @return
     */
    public void update() {
        if (StringUtil.isEmpty(getIpAddr())) {
            this.setIpAddr(null);
        }
        Server server = findExistedByIpAddr(getIpAddr());
        if (null != server && (server.getId().intValue() != this.getId().intValue())) {
            Shift.fatal(BasicRestStatus.repeatDataErrorStatus);
        }
        getServerRepository().updateByPrimaryKeySelective(this);
    }

    /* ******************************************************************************* *
                                            查
     * ******************************************************************************* */

    /**
     * 根据主键ID查询Client
     *
     * @param id
     * @return
     */
    public static Server selectDetailById(int id) {
        Server entity = getServerRepository().selectByPrimaryKey(id);
        return entity;
    }

    /**
     * 根据IpAddr查询Client
     *
     * @param ipAddr
     * @return
     */
    public static Server selectDetailByIpAddr(String ipAddr) {
        Example e = new Example(Server.class);
        e.createCriteria().andEqualTo("ipAddr", ipAddr);
        List<Server> list = getServerRepository().selectByExample(e);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据查询条件查询分页列表
     *
     * @param page
     * @return
     */
    public PageResult<Server> selectPageByQuery(PageResult page) {
        ServerPageQueryDTO dto = new ServerPageQueryDTO(getOperatorId(),
                getName(), getIpAddr(), getIsWork());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<Server> pageInfo = new PageInfo<>(getServerRepository().selectPageByQuery(dto));
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());
        return page;
    }

}